package com.twu.biblioteca;

import com.twu.biblioteca.command.*;

import java.util.*;

public class BibliotecaApp
{
    public static final String SORRY_WE_DON_T_HAVE_THAT_BOOK_YET = "Sorry we don't have that book yet.";
    public static final String ENJOY_THE_BOOK = "Enjoy the book";
    public static final String WELCOME_TO_BIBLIOTECA = "Welcome to Biblioteca";
    public static final String MENU = "    ****************"
            + "\n    1.Show all book"
            + "\n    2.Rent a book"
            + "\n    3.Check my membership detail"
            + "\n    4.Show movies"
            + "\n    5.Login"
            + "\n    6.Quit"
            + "\n    ****************";
    public static final String ENGLISH = "English";
    public static final String MATH = "Math";
    public static final String CHINESE = "Chinese";
    public static final String MUSIC = "Music";
    public static final String SELECT_A_VALID_OPTION = "Select a valid option!!";
    public static final String PLEASE_TALK_TO_A_LIBRARIAN_THANK_YOU = "Please talk to a Librarian. Thank you.";
    public static final String PLEASE_LOGIN_BEFORE_YOU_RENT = "please login before you rent";
    private List<Book> bookList;
    private List<Movie> movieList;
    private List<User> userList;
    private User user;
    private Map<Integer, Command> commandMap;


    public static void main(String[] args)
    {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        bibliotecaApp.runApp();
    }

    public void runApp()
    {
        Scanner sin = new Scanner(System.in);
        welcome();
        while (true) {
            showMenu();
            Command command = selectCommand(sin.nextInt());
            executeCommand(command);
        }
    }

    public BibliotecaApp()
    {
        init();
    }

    private void init()
    {
        commandMap = new HashMap();
        commandMap.put(1, new ShowBookListCommand());
        commandMap.put(2, new RentBookCommand());
        commandMap.put(3, new ShowUserDetailCommand());
        commandMap.put(4, new ShowMovieListCommand());
        commandMap.put(5, new LoginCommand());
        commandMap.put(6, new QuitCommand());

        bookList = new ArrayList();
        bookList.add(new Book(1, ENGLISH, true));
        bookList.add(new Book(2, MATH, true));
        bookList.add(new Book(3, CHINESE, false));
        bookList.add(new Book(4, MUSIC, true));

        movieList = new ArrayList();
        movieList.add(new Movie("Forrest Gump", 1980, "James Lebron", 1));
        movieList.add(new Movie("勇敢的心", 1980, "James Lebron", 5));
        movieList.add(new Movie("天空之城", 1980, "James Lebron", 6));
        movieList.add(new Movie("西游记", 1980, "James Lebron", 9));
        movieList.add(new Movie("生化危机", 1980, "James Lebron", 10));
        movieList.add(new Movie("阿甘正传", 1980, "James Lebron"));
        movieList.add(new Movie("肖声克的救赎", 1999, "James Lebron"));
        movieList.add(new Movie("拯救大兵", 2001, "James Lebron"));
        movieList.add(new Movie("钢铁侠", 2004, "James Lebron", 4));
        movieList.add(new Movie("机器侠", 2003, "James Lebron", 2));
        movieList.add(new Movie("机器猫", 1998, "James Lebron", 8));
        movieList.add(new Movie("忍者神龟", 2011, "James Lebron", 7));
        movieList.add(new Movie("人在囧途", 2009, "James Lebron", 5));
        movieList.add(new Movie("想你", 2000, "James Lebron", 4));
        movieList.add(new Movie("星河战队", 1966, "James Lebron", 3));

        userList = new ArrayList<User>();
        userList.add(new User("111-1111", "yangliu", "yeah_yangliu", "1238784"));
        userList.add(new User("111-1112", "yangliu", "yeah_yangliu", "1238784"));
        userList.add(new User("111-1113", "yangliu", "yeah_yangliu", "1238784"));
        userList.add(new User("111-1114", "yangliu", "yeah_yangliu@163.com", "1238784"));
    }

    public void welcome()
    {
        System.out.println(getWelcomeMessage());
    }

    public void showMenu()
    {
        System.out.println(getMenuList());
        System.out.print("please select:");
    }

    private Command selectCommand(int commandNumber)
    {
        return commandMap.get(commandNumber);
    }

    public void executeCommand(Command command)
    {
        command.execute(this);
    }

    public void showUserDetail()
    {
        System.out.println(achieveUserDetail());
    }

    public String achieveUserDetail()
    {
        if (user != null) {
            return "username:" + getUserName() + "  email:" + getUserEmail() + "  phone:" + getUserPhone();
        } else {
            return PLEASE_TALK_TO_A_LIBRARIAN_THANK_YOU;
        }
    }

    public void login()
    {
        String username;
        String password;
        Scanner sin = new Scanner(System.in);
        System.out.print("input your username:");
        username = sin.nextLine();
        System.out.print("input your password:");
        password = sin.nextLine();

        if (validUser(username, password)) {
            System.out.println("login success");
        } else {
            System.out.println("login faild");
        }

    }

    public List showMovieList()
    {
        System.out.println("    Movies         Year        Director       Rate   ");
        for (Movie movie : movieList) {
            System.out.print("     " + movie.getName() + "       " + movie.getYear() + "      " + movie.getDirector());
            if (movie.getRate() == 0) {
                System.out.println("     N/A");
            } else {
                System.out.println("     " + movie.getRate());
            }
        }
        return movieList;
    }

    public void rentBook()
    {
        if (user != null) {
            Scanner sin = new Scanner(System.in);
            System.out.print("please input bookId:");
            rentBookById(sin.nextInt());
        } else {
            System.out.println(PLEASE_LOGIN_BEFORE_YOU_RENT);
        }
    }

    protected String connectToLibrarian()
    {
        System.out.println(PLEASE_TALK_TO_A_LIBRARIAN_THANK_YOU);
        return PLEASE_TALK_TO_A_LIBRARIAN_THANK_YOU;
    }

    public String uselessCommand()
    {
        System.out.println(SELECT_A_VALID_OPTION);
        return SELECT_A_VALID_OPTION;
    }

    public void rentBookById(int id)
    {
        for (Book book : bookList) {
            if (book.getId() == id) {
                if (book.isRentable()) {
                    System.out.println(ENJOY_THE_BOOK);
                } else {
                    System.out.println(SORRY_WE_DON_T_HAVE_THAT_BOOK_YET);
                }

            }
        }
    }

    public List showBookList()
    {
        for (Book book : bookList) {
            System.out.println(book);
        }
        return bookList;
    }

    public String getMenuList()
    {
        return MENU;
    }

    public String getWelcomeMessage()
    {
        return WELCOME_TO_BIBLIOTECA;
    }

    public void quit()
    {
        System.exit(0);
    }

    public Boolean validUser(String userName, String passWord)
    {
        this.user = null;
        for (User user : userList) {
            if (user.valid(userName, passWord)) {
                this.user = user;
                return true;
            }
        }
        return false;
    }

    public String getUserEmail()
    {
        return user.getEmail();
    }

    public String getUserPhone()
    {
        return user.getPhone();
    }

    public String getUserName()
    {
        return user.getUsername();
    }
}